﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ProgramMain.Database;
using ProgramMain.Framework;

namespace ProgramMain.OpticNet
{
    public delegate bool DAddFibrePoint(int fibreIn, int fibreOut, int vertexID);
    public partial class FibrePointCreate : BaseDetailCtl
    {
        public FibrePointCreate()
        {
            AddFibrePoint = null;
            InitializeComponent();
        }
        public event ShowDetailEventHandler ViewRowIDChanged;

        private Nullable<Int32> m_ViewRow_ID;
        /// <summary>
        /// Фильтр по объекту
        /// </summary>
        public Nullable<Int32> ViewRowID
        {
            get { return m_ViewRow_ID; }
            set
            {
                if (m_ViewRow_ID != value)
                {
                    m_ViewRow_ID = value;
                    ShowDetailEventArgs ed = new ShowDetailEventArgs(m_ViewRow_ID, (EnableEdit) ? ShowDetailModes.Edit : ShowDetailModes.ViewOnly, "", null);
                    OnViewRowIDChanged(ed);
                }
            }
        }

        protected virtual void OnViewRowIDChanged(ShowDetailEventArgs e)
        {
            if (ViewRowIDChanged != null)
                ViewRowIDChanged(this, e);
        }

        private int _vertexID;
        public int VertexID
        {
            get { return _vertexID; }
            set { _vertexID = value; }
        }

        public DAddFibrePoint AddFibrePoint { get; set; }

        private void _loadFibreMain(int? equipID)
        {
            try
            {
                fibreTableAdapter.Fill(simpleOpticNetMain.Fibre, ViewRowID, _curveSecondId, null, true, VertexID);
                
            }
            catch (Exception)
            {

            }

        }

        private void _loadFibreSecond(int? equipID)
        {
            try
            {
                fibreTableAdapter.Fill(simpleOpticNetSecond.Fibre, _curveSecondId, ViewRowID, null, true, VertexID);
            }
            catch (Exception)
            {
            }
        }

        private int _curveSecondId { get; set; }
        private int? _curveElementKindID { get; set; }

        protected override void LoadData()
        {
            curveElementMain.Fill(simpleOpticNetMain.CurveElement, VertexID, DetailID, null, null);
            if (EquipMain.Count > 0)
            {
                EquipMain.MoveFirst();
                if (EquipMain.Current != null)
                lookEquipMain.EditValue = ((SimpleOpticNetDB.CurveElementRow) ((DataRowView) EquipMain.Current).Row).ID;
            }
        }

        public string VertexName
        {
            get { return vertexName.Text; }
            set { vertexName.Text = value; }
        }
        private void _loadCurveSecond()
        {
            int? selectEquip = null;
            if (lookEquipMain.EditValue is int)
            {
                selectEquip = (int) lookEquipMain.EditValue;
            }
            curveElementMain.Fill(simpleOpticNetSecond.CurveElement, VertexID, null, null, selectEquip);
            EquipSecond.Filter = ViewRowID.HasValue ? string.Format("ID <> {0}", ViewRowID) : "";
            if (EquipSecond.Count > 0)
            {
                EquipSecond.MoveFirst();
                if (EquipSecond.Current != null)
                lookEquipSecond.EditValue =
                    ((SimpleOpticNetDB.CurveElementRow) ((DataRowView) EquipSecond.Current).Row).ID;
            }
        }

        public void OnLoadData(object sender, EventArgs e)
        {
            //LoadData();
            _loadFibreMain(ViewRowID);
            _loadFibreSecond(_curveSecondId);
            while(fibreSecond.Count == 0 && EquipSecond.Position + 1 < EquipSecond.Count)
            {
                EquipSecond.MoveNext();
                if( EquipSecond.Current != null)
                lookEquipSecond.EditValue = ((SimpleOpticNetDB.CurveElementRow)((DataRowView)EquipSecond.Current).Row).ID;
                else lookEquipSecond.EditValue = null;
              
            }
            if (fibreSecond.Count == 0 && EquipSecond.Position + 1 == EquipSecond.Count) 
                lookEquipSecond.EditValue = null;
        }

        private void lookEquipMain_EditValueChanged(object sender, EventArgs e)
        {
            if (lookEquipMain.EditValue is int)
            {
                ViewRowID = (int) lookEquipMain.EditValue;
                int p = EquipMain.Find("ID", lookEquipMain.EditValue);
                if (p > -1) EquipMain.Position = p;
            }
            if (EquipMain.Current != null)
            _curveElementKindID =
                ((SimpleOpticNetDB.CurveElementRow)((DataRowView)EquipMain.Current).Row).CurveElementKindID;
            switch (_curveElementKindID)
            {
                case 1:
                    AddMufta.Enabled = true;
                    AddCross.Enabled = true;
                    break;
                case 2:
                    AddMufta.Enabled = true;
                    AddCross.Enabled = false;
                    break;
                case 3:
                    AddMufta.Enabled = false;
                    AddCross.Enabled = false;
                    break;
            }

            _loadCurveSecond();
             if(ViewRowID.HasValue) _loadFibreMain(ViewRowID);
           OnLoadData(null, null);
        }

        private void lookEquipSecond_EditValueChanged(object sender, EventArgs e)
        {
           
            if (lookEquipSecond.EditValue is int)
            {
                _curveSecondId = (int) lookEquipSecond.EditValue;
                _loadFibreSecond((int) lookEquipSecond.EditValue);
            }
            if (ViewRowID.HasValue) _loadFibreMain(ViewRowID);
        }

        private void simpleButton4_Click(object sender, EventArgs e)
        {
            LoadData();
            _loadCurveSecond();
        }

        private void AddMufta_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            CableBoxList cableBoxList = new CableBoxList();
            int? equip = cableBoxList.AddCableBox(VertexID);
            if (equip.HasValue)
            {
                //  _loadCurveSecond();
                LoadData();
                _loadCurveSecond();
                lookEquipSecond.EditValue = equip;
            }
        }

        private void AddCross_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            EquipmentList equipmentList = new EquipmentList();
            int? equip = equipmentList.AddEquipment(VertexID);
            if (equip.HasValue)
            {
                //  _loadCurveSecond();
                LoadData();
                _loadCurveSecond();
                lookEquipSecond.EditValue = equip;
            }
        }

        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (AddFibrePoint != null && lookFibreMain.SelectedValue is int && lookFibreSecond.SelectedValue is int)
                try
                {
                    if (AddFibrePoint((int)lookFibreMain.SelectedValue, (int)lookFibreSecond.SelectedValue, VertexID))
                    {
                        OnLoadData(null, null);
                    }
                }
                catch
                {
                    XtraMessageBox.Show("Произошла ошибка при варки волокн", "Ошибка", MessageBoxButtons.OK,
                                        MessageBoxIcon.Error);
                }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            popupMenu1.ShowPopup(barManager1, simpleButton1.PointToScreen(simpleButton1.Location));
        }

        private void fibreBrush_Click(object sender, EventArgs e)
        {
            if (AddFibrePoint != null && lookFibreMain.SelectedValue is int && lookFibreSecond.SelectedValue is int)
                try
                {
                    if (AddFibrePoint((int)lookFibreMain.SelectedValue, (int)lookFibreSecond.SelectedValue, VertexID))
                    {
                        OnLoadData(null, null);
                    }
                }
                catch
                {
                    XtraMessageBox.Show("Произошла ошибка при варки волокн", "Ошибка", MessageBoxButtons.OK,
                                        MessageBoxIcon.Error);
                }
        }
    }
}
